<?php

#################################################################
#								HTML
#################################################################

/**
 * Text field component registed as 'element/text'
 * 
 * $vars['name']
 * $vars['text']
 * $vars['rich']
 * $vars['config']
 * $vars['description']
 * $vars['blank']
 * $vars['class']
 * $vars['width']
 * $vars['height']
 * $vars['resize']
 * $vars['id']
 * $vars['onChange']
 */

if($_CONTEXT=='HTML'){

	if($vars['rich']){
		$_CSS_STACK->push(blg_get_site_url().'element/text.css', TRUE);
		$_JS_STACK->push(blg_get_site_url().'mod/ckeditor/adapters/jquery.js', TRUE);		
		$_JS_STACK->push(blg_get_site_url().'mod/ckeditor/ckeditor.js', TRUE);
		$_JS_STACK->push(blg_get_site_url().'element/text.js', TRUE);
	} else {
		$_CSS_STACK->push(blg_get_site_url().'element/text.css', TRUE);
		$_JS_STACK->push(blg_get_site_url().'element/text.js', TRUE);		
	}	

	//PREPARE DEFAULTS
	if(empty($vars['id']))
		$vars['id']= 'text_'.random_string(12);
		
	if(!is_bool($vars['rich']))
		$vars['rich']= FALSE;

	if(empty($vars['blank']) && !empty($vars['description'])){
		$vars['blank']= trim(one_line_code(strip_tags($vars['description'])));
	}
	
	if(!is_bool($vars['resize']))
		$vars['resize']= FALSE;	
		
	// TEXTAREA
	if($vars['rich']===FALSE){
		if(empty($vars['text']))
			$class= 'blg_textarea blg_textarea_empty';
		else
			$class= 'blg_textarea';
		
		$text= '<textarea ';
		
		if(!empty($vars['name']))
			$text.= ' name="'.$vars['name'].'"';

		if(!empty($vars['id']))
			$text.= ' id="'.$vars['id'].'" blg_id="'.$vars['id'].'"';

		if(!empty($vars['class']))
			$text.= ' class="'.$class.' '.$vars['class'].'"';
		else
			$text.= ' class="'.$class.'"';			
			
		if(!empty($vars['width']) || !empty($vars['height']) || !$vars['resize']){
			$text.= ' style="';
			
			if(!empty($vars['width']) && is_int($vars['width']))
				$text.= 'width:'.$vars['width'].'px;';
			elseif(!empty($vars['width']))
				$text.= 'width:'.$vars['width'].';';
		
			if(!empty($vars['height']) && is_int($vars['height']))
				$text.= 'height:'.$vars['height'].'px;';
			elseif(!empty($vars['height']))
				$text.= 'height:'.$vars['height'].';';
				
			if(!$vars['resize'])
				$text.= 'resize: none;';
				
			$text.= '"';
		}

		if(!empty($vars['description']))
			$text.= ' description="'.sanitize_for_html_attr($vars['description']).'"';
			
		if(!empty($vars['blank']))
			$text.= ' blank="'.sanitize_for_html_attr($vars['blank']).'"';			

		$text.= '>';
		
		if(!empty($vars['text']))
			$text.= $vars['text'];
		elseif(!empty($vars['blank']))
			$text.= $vars['blank'];
		
		$text.= '</textarea>'."\n";		
	
		//BUILD	
		echo $text;
		
		if(!empty($vars['description'])){
			echo blg_view('element/tipsy', array(
				'id' => $vars['id'],
				'title' => 'description',
				'gravity' => 's',
				'trigger' => 'manual'
			));
			
			$code="			
				$('#".$vars['id']."').bind('mouseenter', function(){
					$(this).tipsy('show');
				});
				$('#".$vars['id']."').bind('mouseleave', function(){
					$(this).tipsy('hide');
				});

				$('#".$vars['id']."').bind('focus', function(){
					$(this).unbind('mouseenter');
					$(this).unbind('mouseleave');
					$(this).tipsy('show');
				});
				$('#".$vars['id']."').bind('blur', function(){
					$(this).tipsy('hide');

					$(this).bind('mouseenter', function(){
						$(this).tipsy('show');
					});
					$(this).bind('mouseleave', function(){
						$(this).tipsy('hide');
					});					
				});				
			";
			
			$_JS_CODE->append($code, ON_DOC_READY);					
		}
		
		if(!empty($vars['onChange'])){
			$code="
				$('#".$vars['id']."').bind('change keyup focus blur click', function(){
					".add_trainling_slash($vars['onChange'], ';')."
				});
			";
			
			$_JS_CODE->append($code, ON_DOC_READY);				
		}
	}
	
	// EDITOR
	if($vars['rich']===TRUE){
		$class= 'blg_editor';
		
		$text= '<textarea ';
		
		if(!empty($vars['name']))
			$text.= ' name="'.$vars['name'].'"';

		if(!empty($vars['id']))
			$text.= ' id="'.$vars['id'].'" blg_id="'.$vars['id'].'"';

		if(!empty($vars['class']))
			$text.= ' class="'.$class.' '.$vars['class'].'"';
		else
			$text.= ' class="'.$class.'"';			
			
		if(!empty($vars['width']) || !empty($vars['height']) || !$vars['resize']){
			$text.= ' style="';
			
			if(!empty($vars['width']) && is_int($vars['width']))
				$text.= 'width:'.$vars['width'].'px;';
			elseif(!empty($vars['width']))
				$text.= 'width:'.$vars['width'].';';
		
			if(!empty($vars['height']) && is_int($vars['height']))
				$text.= 'height:'.$vars['height'].'px;';
			elseif(!empty($vars['height']))
				$text.= 'height:'.$vars['height'].';';
				
			if(!$vars['resize'])
				$text.= 'resize: none;';
				
			$text.= '"';
		}

		if(!empty($vars['description']))
			$text.= ' description="'.sanitize_for_html_attr($vars['description']).'"';

		$text.= '>';
		
		if(!empty($vars['text']))
			$text.= $vars['text'];
		
		$text.= '</textarea>'."\n";		
	
		//BUILD	
		echo $text;		
		
		//EDITOR CONFIG
		$config= array();
		require(blg_get_site_path().'mod'.DS.'ckeditor'.DS.'blg'.DS.'config.php');
		
		$code= "
			$('#".$vars['id']."').ckeditor( 
				function(){}, 
				".$config."		
			);
		";		
		
		$_JS_CODE->append($code, ON_DOC_READY);
	}

}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

$(document).ready(function(){
	/** TEXTAREA **/
	$('.blg_textarea[blank]').focus(function(){
		if($(this).val()==$(this).attr('blank'))
			$(this).val('');
		$(this).addClass('blg_textarea_focused');
		$(this).removeClass('blg_textarea_empty');
	});
	
	$('.blg_textarea[blank]').blur(function(){
		if($(this).val()==''){
			$(this).val($(this).attr('blank'));
			$(this).addClass('blg_textarea_empty');
		}
		$(this).removeClass('blg_textarea_focused');	
	});
		
	/** EDITOR **/
	$('.blg_editor').focus(function(){
		$(this).addClass('blg_textarea_focused');
	});
	
	$('.blg_editor').blur(function(){		
		$(this).removeClass('blg_textarea_focused');	
	});
});

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

textarea.blg_textarea, textarea.blg_editor{		
	color: <?php echo blg_color('grey', '5');?>;
	border: 1px solid <?php echo blg_color('grey', 'D');?>;    
    border-radius: 3px;
    
    font-size: 13px;
    font-family: 'Mako',Arial,Helvetica,sans-serif;
    padding: 2px 2px;
}
textarea.blg_textarea:hover, textarea.blg_editor:hover{
	border: 1px solid <?php echo blg_color('grey', 'B');?>;
}

textarea.blg_textarea_focused{
	color: <?php echo blg_color('grey', '0');?>;
	border: 1px solid <?php echo blg_color('blue', 'light');?>;
}
textarea.blg_textarea_focused:hover{
	border: 1px solid <?php echo blg_color('blue', 'light');?>;
}

textarea.blg_textarea_empty{
	color: <?php echo blg_color('grey', 'B');?>;
}

<?php }
